home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Open Source / AutoHotKey / Source / AutoHotkey104705_source.exe / source / SimpleHeap.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-01-01  |  2.9 KB  |  63 lines

  1. /*
  2. AutoHotkey
  3.  
  4. Copyright 2003-2007 Chris Mallett (support@autohotkey.com)
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15. */
  16.  
  17. #ifndef SimpleHeap_h
  18. #define SimpleHeap_h
  19.  
  20. #include "stdafx.h" // pre-compiled headers
  21.  
  22. // In a large script (200 KB of text) of a typical nature, using SimpleHeap rather than malloc() saves
  23. // nearly 200 KB of memory as shown by Task Manager's "VM Size" column (2384 vs. 2580 KB).
  24. // This is because many callers allocate chunks of memory that are very small on average.  If each
  25. // such chunk were allocated with malloc (or worse, "new"), the percentage of system overhead
  26. // compared to the memory actually used for such blocks would be enormous (perhaps 40 bytes of
  27. // overhead for each malloc(), even if it's only for 3 or 4 bytes).  In addition, SimpleHeap improves
  28. // performance to the extent that it is faster than malloc(), which it almost certainly is.  Finally,
  29. // the OS's overall memory fragmentation may be reduced, especially if the app uses this class over
  30. // a long period of time (hours or days).
  31.  
  32. // The size of each block in bytes.  Use a size that's a good compromise
  33. // of avg. wastage vs. reducing memory fragmentation and overhead.
  34. // But be careful never to reduce it to something less than LINE_SIZE
  35. // (the maximum line length that can be loaded -- currently 16K), otherwise,
  36. // memory for that line might be impossible to allocate.
  37. // Update: reduced it from 64K to 32K since many scripts tend to be small.
  38. #define BLOCK_SIZE (32 * 1024) // Relied upon by Malloc() to be a multiple of 4.
  39.  
  40. class SimpleHeap
  41. {
  42. private:
  43.     char *mBlock; // This object's memory block.  Although private, its contents are public.
  44.     char *mFreeMarker;  // Address inside the above block of the first unused byte.
  45.     size_t mSpaceAvailable;
  46.     static UINT sBlockCount;
  47.     static SimpleHeap *sFirst, *sLast;  // The first and last objects in the linked list.
  48.     static char *sMostRecentlyAllocated; // For use with Delete().
  49.     SimpleHeap *mNextBlock;  // The object after this one in the linked list; NULL if none.
  50.  
  51.     static SimpleHeap *CreateBlock();
  52.     SimpleHeap();  // Private constructor, since we want only the static methods to be able to create new objects.
  53.     ~SimpleHeap();
  54. public:
  55.     static UINT GetBlockCount() {return sBlockCount;}
  56.     static char *Malloc(char *aBuf, size_t aLength = -1); // Return a block of memory to the caller and copy aBuf into it.
  57.     static char *Malloc(size_t aSize); // Return a block of memory to the caller.
  58.     static void Delete(void *aPtr);
  59.     //static void DeleteAll();
  60. };
  61.  
  62. #endif
  63.